//
//  LANSearchManager.m
//  SiterLib
//
//  Created by tracyhenry on 2022/7/6.
//

#import "LANSearchManager.h"
#import "AppUdp.h"

typedef void(^SearchCallback)(id response,NSError *error);

@interface LANSearchManager()<SiterDiscoverDelegate>
@property(nonatomic,strong)AppUdp *appUdp;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,strong)SearchCallback searchCallback;
@property(nonatomic,assign)int lan_count;
@property(nonatomic,strong)NSMutableArray * mac_array;
@property(nonatomic,strong)NSMutableArray * mac_array_dic;
@end

@implementation LANSearchManager

+ (instancetype)sharedInstanced {
    static LANSearchManager* lansearchManager;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        lansearchManager = [[LANSearchManager alloc] init];
        lansearchManager.appUdp = [AppUdp shared];
        lansearchManager.appUdp.delegate_discover = lansearchManager;
    });
    return lansearchManager;
}


-(NSMutableArray *)mac_array{
    if(_mac_array==nil){
        _mac_array = [[NSMutableArray alloc] init];
    }
    return _mac_array;
}

-(NSMutableArray *)mac_array_dic{
    if(_mac_array_dic==nil){
        _mac_array_dic = [[NSMutableArray alloc] init];
    }
    return _mac_array_dic;
}


-(void)startSearchLan:(void(^)(id,NSError *))handler{
    
    if(_timer!=nil){
        [_timer invalidate];
        _timer = nil;
    }
    [self.mac_array removeAllObjects];
    [self.mac_array_dic removeAllObjects];
    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(adddone) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode];
    
    self.lan_count = 0;
    self.searchCallback = handler;
}

-(void)stopSearch{
    [self.mac_array removeAllObjects];
    [self.mac_array_dic removeAllObjects];
    if(self.timer!=nil){
        self.lan_count = 0;
        [self.timer invalidate];
        self.timer = nil;
    }
}

-(void) adddone{
    
    if(self.lan_count<=2){
        [self.appUdp sendBroadcast];
    }else{
        [self.appUdp sendBroadcastSubsection:self.lan_count/3-1];
    }
    self.lan_count++;
    if(self.lan_count>=768){
        self.lan_count = 0;
        if(_timer!=nil){
            [_timer invalidate];
            _timer = nil;
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:5000 userInfo:nil];
            self.searchCallback(self.mac_array_dic,error);
        }
    }
}

-(void)deviceDiscover:(NSString *)mac withIp:(NSString *)ip{
    if(![self.mac_array containsObject:mac]){
        [self.mac_array addObject:mac];
        [self.mac_array_dic addObject:@{@"mac":mac,@"ip":ip}];
        if(self.searchCallback!=nil){
            self.searchCallback(self.mac_array_dic,nil);
        }
    }
}

-(void)dealloc{
    [self.mac_array removeAllObjects];
    [self.mac_array_dic removeAllObjects];
    self.mac_array = nil;
    self.mac_array_dic = nil;
    self.lan_count = 0;
    if(_timer!=nil){
        [_timer invalidate];
        _timer = nil;
    }
}
@end
